home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / util / misc / VMM_src.lha / VMM / stat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-16  |  8.3 KB  |  325 lines

  1. #include <exec/types.h>
  2. #include "defs.h"
  3.  
  4. static char rcsid [] = "$Id: stat.c,v 3.6 95/12/16 18:36:56 Martin_Apel Exp $";
  5.  
  6. #define MARGIN 10L
  7.  
  8. PRIVATE struct Window *StatWin;
  9.  
  10. PRIVATE WORD ZoomVals [] =
  11.   {
  12.   0, 0,
  13.   0, 0                /* Dummy values, filled in later */
  14.   };
  15.  
  16. PRIVATE char WinTitle [40];
  17.  
  18. PRIVATE struct TagItem WindowTags [] = 
  19.   { { WA_Left, 0 },
  20.     { WA_Top, 0 },
  21.     { WA_Width, 0 },
  22.     { WA_Height, 0 },   /* Dummy values, filled in later */
  23.     { WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_CHANGEWINDOW },
  24.     { WA_Title, (ULONG)WinTitle},
  25.     { WA_CloseGadget, TRUE },
  26.     { WA_DragBar, TRUE },
  27.     { WA_NoCareRefresh, TRUE },
  28.     { WA_SimpleRefresh, TRUE },
  29.     { WA_DepthGadget, TRUE },
  30.     { WA_Zoom, (ULONG)ZoomVals },
  31.     { TAG_DONE, 0 }
  32.   };
  33.  
  34. PRIVATE int LineSpacing;
  35. PRIVATE int TitleHeight;
  36. PRIVATE int TitleWidth;
  37. PRIVATE UWORD TimerSignal;
  38. PRIVATE void *TimerStruct;
  39. PRIVATE int max_length = 0;
  40.  
  41. #ifdef SCHED_STAT
  42. #ifdef DEBUG
  43. #define NUM_LINES 12
  44. #else
  45. #define NUM_LINES 10
  46. #endif
  47. #else
  48. #define NUM_LINES 7
  49. #endif
  50.  
  51. #define CURRENTLY_ZOOMED (StatWin->Height == TitleHeight + 3)
  52.  
  53. PRIVATE char *StatStrings [NUM_LINES];
  54.  
  55. /******************************************************************/
  56.  
  57. PRIVATE void PrintStatMsg (int y, char *string, long val)
  58.  
  59. {
  60. char buffer [80];
  61. int i;
  62.  
  63. strcpy (buffer, string);
  64.  
  65. for (i = strlen (string); i <= max_length; i++)
  66.   buffer [i] = ' ';
  67.  
  68. sprintf (&(buffer [max_length+1]), "%8ld", val);
  69.  
  70. Move (StatWin->RPort, MARGIN, (long)y * LineSpacing + TitleHeight + 2);
  71. Text (StatWin->RPort, buffer, (ULONG)strlen (buffer));
  72. }
  73.  
  74. /******************************************************************/
  75.  
  76. PRIVATE void PrintFreeVirtMem (void)
  77.  
  78. {
  79. #ifdef SCHED_STAT
  80. struct TrapStruct *ThisFault;
  81. int i;
  82. #endif
  83.  
  84. int line_no = 1;
  85.  
  86. if ((CurrentConfig.StatZoomed && !CURRENTLY_ZOOMED) ||
  87.     (!CurrentConfig.StatZoomed && CURRENTLY_ZOOMED))
  88.   ZipWindow (StatWin);
  89.  
  90. if (!CURRENTLY_ZOOMED)
  91.   {
  92.   PrintStatMsg (line_no++, StatStrings [0], VirtMem->mh_Free);
  93.   PrintStatMsg (line_no++, StatStrings [1], 
  94.                 DoOrigAvailMem (MEMF_PUBLIC|MEMF_FAST));
  95.   PrintStatMsg (line_no++, StatStrings [2], NumPageFaults);
  96.   PrintStatMsg (line_no++, StatStrings [3], PagesRead);
  97.   PrintStatMsg (line_no++, StatStrings [4], PagesWritten);
  98.   PrintStatMsg (line_no++, StatStrings [5], NumPageFrames);
  99.   PrintStatMsg (line_no++, StatStrings [6], SlotsUsed ());
  100. #ifdef SCHED_STAT
  101.   PrintStatMsg (line_no++, StatStrings [7], FramesExamined);
  102.  
  103.   i = 0;
  104.   for (ThisFault = (struct TrapStruct*)Free.lh_Head; 
  105.        ThisFault->TS_Node.mln_Succ != NULL; 
  106.        ThisFault = (struct TrapStruct*) ThisFault->TS_Node.mln_Succ)
  107.     i++;
  108.  
  109.   PrintStatMsg (line_no++, StatStrings [8], (long)i);
  110.  
  111.   PrintStatMsg (line_no++, StatStrings [9], AddedMemSize - 
  112.                 VirtMem->mh_Free - sizeof (struct MemHeader));
  113.  
  114. #ifdef DEBUG
  115.   PrintStatMsg (line_no++, StatStrings [10], EnforcerHits);
  116.   PrintStatMsg (line_no++, StatStrings [11], InstructionFaults);
  117. #endif
  118. #endif
  119.   }
  120. else
  121.   {
  122.   sprintf (WinTitle, "VM: %5ld K, Fast: %5ld K", VirtMem->mh_Free / 1024,
  123.            DoOrigAvailMem (MEMF_PUBLIC|MEMF_FAST) / 1024);
  124.   SetWindowTitles (StatWin, WinTitle, (char*)-1);
  125.   }
  126. AddTimedFunction (TimerStruct, 1L, 0L, PrintFreeVirtMem);
  127. }
  128.  
  129. /******************************************************************/
  130.  
  131. PRIVATE int Init_Stat (void)
  132.  
  133. {
  134. struct Screen *DefaultScreen;
  135. int i;
  136.  
  137. StatQuitSignal   = AllocSignal (-1L);
  138.  
  139. DISABLE_VM;
  140. if ((TimerStruct = InitTimer (1, &TimerSignal)) == NULL)
  141.   {
  142.   ReportError (GetVMMString (msgNoTimer), ERR_CONTINUE);
  143.   ENABLE_VM;
  144.   return (ERR_MSG_POSTED);
  145.   }
  146.  
  147. PRINT_DEB ("Timer initialized", 0L);
  148.  
  149. StatStrings [0] = GetVMMString (msgVMFree);
  150. StatStrings [1] = GetVMMString (msgPublicFastFree);
  151. StatStrings [2] = GetVMMString (msgNumPF);
  152. StatStrings [3] = GetVMMString (msgNumRead);
  153. StatStrings [4] = GetVMMString (msgNumWritten);
  154. StatStrings [5] = GetVMMString (msgNumFrames);
  155. StatStrings [6] = GetVMMString (msgPagesUsed);
  156.  
  157. #ifdef SCHED_STAT
  158. StatStrings [7] = "FramesExamined:";
  159. StatStrings [8] = "Trapstructs free:";
  160. StatStrings [9] = "VM used:";
  161. #ifdef DEBUG
  162. StatStrings [10] = "Enforcer hits:";
  163. StatStrings [11] = "InstructionFaults:";
  164. #endif
  165. #endif
  166.  
  167. for (i = 0; i < NUM_LINES; i++)
  168.   {
  169.   if (strlen (StatStrings [i]) > max_length)
  170.     max_length = strlen (StatStrings [i]);
  171.   }
  172.  
  173. /* Window.Width */
  174. Forbid ();
  175. ZoomVals [2] = 
  176. WindowTags [2].ti_Data = GfxBase->DefaultFont->tf_XSize * (max_length + 9)    /* for space and 8 digits */
  177.                          + 2 * MARGIN;
  178. LineSpacing = GfxBase->DefaultFont->tf_YSize + 2;
  179. Permit ();
  180.  
  181. DefaultScreen = LockPubScreen (NULL);
  182. TitleHeight = DefaultScreen->RastPort.Font->tf_YSize;
  183. TitleWidth = TextLength (&(DefaultScreen->RastPort), "VM: 00000 K, Fast: 00000 K", 26L); 
  184.  
  185. strcpy (WinTitle, PROGNAME);
  186.  
  187. if (CurrentConfig.StatZoomed)
  188.   {
  189.   WindowTags [0].ti_Data = CurrentConfig.ZLeftEdge;
  190.   WindowTags [1].ti_Data = CurrentConfig.ZTopEdge;
  191.   WindowTags [2].ti_Data = TitleWidth + 3 * 25;
  192.   WindowTags [3].ti_Data = TitleHeight + 3;
  193.   ZoomVals [0] = CurrentConfig.UnZLeftEdge;
  194.   ZoomVals [1] = CurrentConfig.UnZTopEdge;
  195.   ZoomVals [3] = TitleHeight + (NUM_LINES + 1) * LineSpacing;
  196.   }
  197. else
  198.   {
  199.   WindowTags [0].ti_Data = CurrentConfig.UnZLeftEdge;
  200.   WindowTags [1].ti_Data = CurrentConfig.UnZTopEdge;
  201.   WindowTags [3].ti_Data = TitleHeight + (NUM_LINES + 1) * LineSpacing;
  202.   ZoomVals [0] = CurrentConfig.ZLeftEdge;
  203.   ZoomVals [1] = CurrentConfig.ZTopEdge;
  204.   ZoomVals [2] = TitleWidth + 3 * 25;
  205.   ZoomVals [3] = TitleHeight + 3;
  206.   }
  207.  
  208. PRINT_DEB ("Opening window", 0L);
  209.  
  210. if ((StatWin = OpenWindowTagList (NULL, WindowTags)) == NULL)
  211.   {
  212.   PRINT_DEB ("Couldn't open StatWindow", 0L);
  213.   ENABLE_VM;
  214.   return (ERR_NO_STATWINDOW);
  215.   }
  216.  
  217. UnlockPubScreen (NULL, DefaultScreen);
  218.  
  219. PRINT_DEB ("Window opened", 0L);
  220. SetAPen (StatWin->RPort, 1L);
  221. AddTimedFunction (TimerStruct, 1L, 0L, PrintFreeVirtMem);
  222. ENABLE_VM;
  223. return (SUCCESS);
  224. }
  225.  
  226. /******************************************************************/
  227.  
  228. PRIVATE void Cleanup_Stat (void)
  229.  
  230. {
  231. if (StatWin != NULL)
  232.   CloseWindow (StatWin);
  233.  
  234. CurrentConfig.StatEnabled = FALSE;
  235. CloseTimer (TimerStruct);
  236.  
  237. FreeSignal ((ULONG)StatQuitSignal);
  238.  
  239. PRINT_DEB ("Exiting", 0L);
  240. }
  241.  
  242. /******************************************************************/
  243.  
  244. void Statistics (void)
  245.  
  246. {
  247. ULONG ReceivedSignals;
  248. ULONG WaitMask;
  249. struct IntuiMessage *IMsg;
  250. struct VMMsg *InitMsg;
  251. BOOL quit = FALSE;
  252. int rc;
  253.  
  254. if ((rc = Init_Stat ()) != SUCCESS)
  255.   {
  256.   InitError (rc);
  257.   Cleanup_Stat ();
  258.   Signal ((struct Task*)VM_ManagerProcess, 1L << InitPort->mp_SigBit);
  259.   return;
  260.   }
  261.  
  262. PRINT_DEB ("Initialization ready", 0L);
  263.  
  264. /* Tell VM_Manager that the stat task has been initialized correctly */
  265. if ((InitMsg = DoOrigAllocMem (sizeof (struct VMMsg), MEMF_PUBLIC | MEMF_CLEAR)) == NULL)
  266.   {
  267.   PRINT_DEB ("Couldn't allocate memory for init msg", 0L);
  268.   InitError (ERR_NOT_ENOUGH_MEM);
  269.   Cleanup_Stat ();
  270.   Signal ((struct Task*)VM_ManagerProcess, 1L << InitPort->mp_SigBit);
  271.   return;
  272.   }
  273.  
  274. InitMsg->VMSender = FindTask (NULL);
  275. InitMsg->VMCommand = VMCMD_InitReady;
  276. InitMsg->ReplySignal = NULL;                 /* Let VM_Manager free it */
  277. PutMsg (InitPort, (struct Message*)InitMsg);
  278.  
  279. WaitMask = (1L << TimerSignal) | (1L << StatQuitSignal) |
  280.            (1L << StatWin->UserPort->mp_SigBit);
  281.  
  282. while (!quit)
  283.   {
  284.   PRINT_DEB ("Waiting for signals", 0L);
  285.   ReceivedSignals = Wait (WaitMask);
  286.   PRINT_DEB ("Received signals", ReceivedSignals);
  287.  
  288.   if (ReceivedSignals & (1L << TimerSignal))
  289.     HandleTimerReturn (TimerStruct);
  290.  
  291.   if (ReceivedSignals & (1L << StatQuitSignal))
  292.     quit = TRUE;
  293.  
  294.   if (ReceivedSignals & (1L << StatWin->UserPort->mp_SigBit))
  295.     {
  296.     if ((IMsg = (struct IntuiMessage*)GetMsg (StatWin->UserPort)) != NULL)
  297.       {
  298.       switch (IMsg->Class)
  299.         {
  300.         case IDCMP_CLOSEWINDOW:
  301.              quit = TRUE;
  302.              break;
  303.  
  304.         case IDCMP_CHANGEWINDOW:
  305.              CurrentConfig.StatZoomed = CURRENTLY_ZOOMED;
  306.              if (!CurrentConfig.StatZoomed)
  307.                {
  308.                SetWindowTitles (StatWin, PROGNAME, (char*)-1);
  309.                CurrentConfig.UnZLeftEdge = StatWin->LeftEdge;
  310.                CurrentConfig.UnZTopEdge = StatWin->TopEdge;
  311.                }
  312.              else
  313.                {
  314.                CurrentConfig.ZLeftEdge = StatWin->LeftEdge;
  315.                CurrentConfig.ZTopEdge = StatWin->TopEdge;
  316.                }
  317.              break;
  318.         }
  319.       ReplyMsg ((struct Message*)IMsg);
  320.       }
  321.     }
  322.   }
  323. Cleanup_Stat ();
  324. }
  325.